package cn.bjc.org.controller;

import cn.bjc.basic.annotation.PreAuthorize;
import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.PageList;
import cn.bjc.org.domain.Department;
import cn.bjc.org.query.DepartmentQuery;
import cn.bjc.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller
//@ResponseBody
@RestController //controller层,返回json数据
@RequestMapping("/department")
//接口文档的注解
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
   
    //获取部门树
    @GetMapping("/deptTree")
    @PreAuthorize(name = "部门树",sn = "department:tree") //sn后面用来做权限校验
    public List<Department> deptTree(){
        return departmentService.deptTree();
        
    }
    
    
    /**
     *  查询所有:
     *  动词:get
     * @return
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping()//上面的结合
    @ApiOperation(value = "查询所有")
    @PreAuthorize(name = "所有部门",sn = "department:getAll")
    public List<Department> findAll() {
        return departmentService.loadAll();
    }


    /**
     * 查询一个:接收参数id
     * url:http://localhost/department/id/
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询")
    @PreAuthorize(name = "通过id查询部门",sn = "department:get")
    public Department findOne(@PathVariable("id") long id) {
        return departmentService.loadById(id);
    }

    /**
     * 删除操作
     *  url:http://localhost/department/id/
     * @param id
     * @return
     * JsonResult:链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 添加或修改操作
     * 
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改操作接口")
    @PreAuthorize(name = "保存部门",sn = "department:save")
    public JsonResult updateOrAdd(@RequestBody Department department){
        try {
            if(department.getId()==null){
                //添加操作
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return JsonResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * 
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    @PreAuthorize(name = "部门分页查询",sn = "department:queryPage")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        try {
            return departmentService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<>();
        }
    }


    /**
     * 批量删除操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除")
    @PreAuthorize(name = "批量删除",sn = "department:patchDel")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            departmentService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }
}
